<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 苹果bom管理
 * @author zxj
 *
 */
class Iphonebom extends MY_Controller {
	private $t_bom = 'material_group';  //苹果套料表
    private $iphoneType = array('iphone5','iphone5s','iphone6');
	function __construct() {
		parent::__construct();
	}

	/**
	 * bom列表
	 */
	function index() {
        //获取表单数据
        $data = $formField = gp();
        //查询条件
        $formField['keyword'] = trim($formField['keyword']);
        $searchStr =  'orders/Iphonebom/index/?';
        if($formField['keyword']){
            $where = 'where mg_name like \'%'.$data['keyword'].'%\' or mg_sku like \'%'.$data['keyword'].'%\' ';
            $searchStr .= 'keyword='.$formField['keyword'];
        }
		//分页
		$this->load->library('pagination');
		$config['page_query_string'] = TRUE;
		$config['base_url'] = blang_url($searchStr);
        $config['total_rows'] = $this->common_model->getQuery('select * from material_group '.$where);
		$config['per_page'] = 20;
        if($data['outputExcel']){
            $config['per_page'] = 10000;
            $data['page'] = 1;
        }
        $page = max(1,intval($data['page']));
        $config['uri_segment'] = config_item('enable_lang')?5:4;
		$slimit = ($page - 1) * $config['per_page'];
		$this->pagination->initialize($config);
		$data['show_pages'] = $this->pagination->create_links();
		$num_page = ceil($config['total_rows']/$config['per_page']);
		$data['show_params'] = "总共{$config['total_rows']}条记录   {$config['per_page']}条/页   当前第{$page}/{$num_page}页";
		//查询
		$dataList = $this->common_model->getQuery('select * from material_group '.$where.' order by mg_type desc limit '.$slimit.','.$config['per_page'],'result_array');
        $memoryAry = array('0'=>'通用','1'=>'16GB','2'=>'32GB','3'=>'64GB');
        $colorAry = array('0'=>'通用','1'=>'金色','2'=>'银色','3'=>'灰色');
        foreach ($dataList as &$rs) {
            $rs['memory'] = $memoryAry[$rs['mg_memory']];
            $rs['color'] = $colorAry[$rs['mg_color']];
            $rs['status_name'] = $rs['mg_status']?'正常':'不可用';
            if($rs['is_lock']=='1'){
                $rs['lock_val'] = 'Yes';
            }else if($rs['is_lock']=='2'){
                $rs['lock_val'] = 'No';
            }
        }
        unset($rs);
        $data['dataList'] = $dataList;
		//导出数据
		$data['outputUrl'] = blang_url($searchStr.'&outputExcel=true&page='.$data['page']);
		if ($data['outputExcel']) {
            $fieldData = array(
                'mg_name'=>'名称','mg_type'=>'类型','mg_price'=>'价格','mg_sku'=>'sku',
                'memory'=>'内存','color'=>'颜色','lock_val'=>'是否指纹锁'
            );
            excel_output('iphone_bom'.date('y-m-d'),$fieldData,$dataList);
		}else{
			//视图以及样式
			$this->load->view('defaults/iphonebom_list',$data);
            $this->load->view('defaults/common/footer');
		}
	}


	/**
	 * add
	 */
	function add(){
		//表单处理
		$data['post_type'] = __FUNCTION__;
		//插入数据
        $formData = gp();
		if ($formData['submits']) {
            //获取表字段
            $fieldsList = $this->db->list_fields($this->t_bom);
            //组织插入的数据
            $indata = array();
            foreach ($formData as $key=>$val) {
                if($val!=='' && in_array($key,$fieldsList)){
                    $indata[$key] = $val;
                }
            }
            $this->common_model->insert($this->t_bom,$indata);
            js_goto(blang_url('orders/Iphonebom/'),'新增成功!');
		}else{
            $data['typeList'] = $this->iphoneType;
			//视图以及样式
			$this->load->view('defaults/iphonebom_post',$data);
			$this->load->view('defaults/common/footer');
		}
	}



    /**
     * edit
     * @param string $id
     */
    function edit($id = ''){
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $formData = gp();
        $id = intval(empty($id)?$formData['id']:$id);
        $bomFirst = $this->common_model->selectFirst($this->t_bom,array('id'=>$id));
        $data['bomFirst'] = $bomFirst;
        if ($formData['submits']) {
            //获取表字段
            $fieldsList = $this->db->list_fields($this->t_bom);
            //组织插入的数据
            $indata = array();
            foreach ($formData as $key=>$val) {
                if($val!=='' && in_array($key,$fieldsList)){
                    $indata[$key] = $val;
                }
            }
            $this->common_model->update($this->t_bom,$indata,array('id'=>$id));
            js_goto(blang_url('orders/Iphonebom/'),'修改成功!');
        }else{
            $data['typeList'] = $this->iphoneType;
            $optType[$bomFirst['mg_type']] = 'selected';
            $data['optType'] = $optType;
            $ckdMemory[$bomFirst['mg_memory']] = 'checked';
            $data['ckdMemory'] = $ckdMemory;
            $ckdColor[$bomFirst['mg_color']] = 'checked';
            $data['ckdColor'] = $ckdColor;
            $ckdLock[$bomFirst['mg_lock']] = 'checked';
            $data['ckdLock'] = $ckdLock;

            //视图以及样式
            $this->load->view('defaults/iphonebom_post',$data);
            $this->load->view('defaults/common/footer');
        }
    }




    /**
     * delete
     * @param string $id
     * @param string $page
     */
    function del($id = '',$page = ''){
        $roleAry = array('zlj','zlj2','admin','prezwang','Julie','anna');
        if(!$id || !in_array($this->users['account'],$roleAry)){
            show_error('orders/order/','你没有权限这么做');
        }
        $this->common_model->delete($this->t_bom,array('id'=>$id));
        redirect(blang_url('orders/Iphonebom/index/?page='.$page));
    }


}
